package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public void save(User user) {
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
        if (temp == null) {
            //设值默认头像,背景
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("/img/user/bg.jpeg");
            userMapper.insert(user);
            return;
        }
        throw new RuntimeException("该邮箱已存在");
    }

    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if(user == null){
            throw new RuntimeException("邮箱和密码不匹配");
        }
        return user;
    }

    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<User> list = userMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    public void updat(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
